package ro.imc.ums.dani.database;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import ro.imc.ums.dani.classes.Groups;



public class GroupCrud implements Crud {
	private Connection conn = null;

	public GroupCrud() {

	}

	@Override
	public void insert(Object o) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/ums", "root", "");
			Statement sqlState = conn.createStatement();

			Groups group = (Groups) o;

			String insert = "insert into groups"
					+ " (GroupName, GroupYear, GroupFieldofStudy)"
					+ " values " + "('" + group.getName() + "','"
					+ group.getYear() + "','" + group.getFieldOfStudy() + " ') ";

			sqlState.executeUpdate(insert);
		} catch (SQLException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

	@Override
	public ArrayList<Object> select() {
		ArrayList<Object> groupList = new ArrayList<Object>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/ums", "root", "");
			Statement sqlState = conn.createStatement();
			String select = "select * from groups";
			ResultSet result = sqlState.executeQuery(select);
			while (result.next()) {
				Groups group = new Groups(result.getString(2), Integer.parseInt(result.getString(3)), result.getString(4));
				groupList.add(group);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return groupList;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete() {
		// TODO Auto-generated method stub

	}

}

